//PhotoPreview 点击小图后的效果
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

class ImageShowServerWidget extends StatefulWidget {
  final int initialIndex; //初始图片索引
  final List<dynamic> photoList; //图片数组
  final PageController pageController;

  ImageShowServerWidget({this.initialIndex, this.photoList})
      : pageController = PageController(initialPage: initialIndex);

  @override
  _ImageShowServerWidgetState createState() => _ImageShowServerWidgetState();
}

class _ImageShowServerWidgetState extends State<ImageShowServerWidget> {
  int currentIndex;

  @override
  void initState() {
    currentIndex = widget.initialIndex;
    super.initState();
  }

  //图片切换
  void onPageChanged(int index) {
    setState(() {
      currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Stack(
        children: <Widget>[
          Positioned(
            child: PhotoViewGallery.builder(
              scrollPhysics: const BouncingScrollPhysics(),
              onPageChanged: onPageChanged,
              itemCount: widget.photoList.length,
              pageController: widget.pageController,
              builder: (BuildContext context, int index) {
                return PhotoViewGalleryPageOptions(
                  imageProvider: NetworkImage(widget.photoList[index]), //传入图片
                  minScale: PhotoViewComputedScale.contained * 0.6,
                  maxScale: PhotoViewComputedScale.covered * 1.1,
                  initialScale: PhotoViewComputedScale.contained,
                );
              },
            ),
          ),
          Positioned(
            left: 10,
            top: 60,
            child: GestureDetector(
              child: Row(
                children: <Widget>[
                  Icon(
                    Icons.close,
                    color: Colors.white,
                  ),

                ],
              ),
              onTap: () {
                Navigator.pop(context);
              },
            ),
          ),
          Positioned(
              right: 10,
              bottom: 60,
              child: Row(
                children: <Widget>[
                  Text('${currentIndex + 1}',
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: 18,
                          decoration: TextDecoration.none,
                          fontWeight: FontWeight.w300)),
                  Text('/',
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: 18,
                          decoration: TextDecoration.none,
                          fontWeight: FontWeight.w300)),
                  Text('${widget.photoList.length}',
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: 18,
                          decoration: TextDecoration.none,
                          fontWeight: FontWeight.w300))
                ],
              ))
        ],
      ),
    );
  }
}
